﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Security.Cryptography;
using Controller;
using Model;

namespace Biffen
{
    public partial class MembersUI : Form
    {
        MemberCtr memberCtr = new MemberCtr();
        List<Member> memberList = new List<Member>();
        public MembersUI()
        {
            InitializeComponent();
            getMembers();
        }

        public void getMembers()
        {
            memberList = memberCtr.GetAllMembers();
            foreach (Member member in memberList)
            {
                listMembers.Items.Add(member.MemberNr + "  Navn:" + member.FName + " " + member.LName);
            }

        }

        private void btnEditCreate_Click(object sender, EventArgs e)
        {
            string firstName = txtAddFirstName.Text;
            string lastName = txtAddLastName.Text;
            string telephoneNr = txtPhoneNr.Text;
            string email = txtAddEmail.Text;
            string password = txtAddPassword.Text;
            string md5password = CalculateMD5Hash(password);
            byte newsletter = 0;
            byte specialoffer = 0;
            if (cbNewLetter.Checked)
            {
                newsletter = 1;
            }
            if (cbSpecialOffer.Checked)
            {
                specialoffer = 1;
            }
            Member memberObj = new Member(firstName, lastName, email, telephoneNr, newsletter, specialoffer, md5password);
            string resultText = memberCtr.CreateMember(memberObj);
            lblResult.Text = resultText;
        }

        public string CalculateMD5Hash(string input)
        {
            // step 1, calculate MD5 hash from input
            MD5 md5 = System.Security.Cryptography.MD5.Create();
            byte[] inputBytes = System.Text.Encoding.ASCII.GetBytes(input);
            byte[] hash = md5.ComputeHash(inputBytes);

            // step 2, convert byte array to hex string
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < hash.Length; i++)
            {
                sb.Append(hash[i].ToString("X2"));
            }
            return sb.ToString();
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            int id = listMembers.SelectedIndex;
            Member memberObj = memberList[id];
            string result = memberCtr.DeleteMember(memberObj.Id);
            lblStatus.Text = result;
        }

        private void listMembers_SelectedIndexChanged(object sender, EventArgs e)
        {
            int id = listMembers.SelectedIndex;
            if (id >= 0 && memberList != null)
            {
                Member memberObj = memberList[id];

                txtEditEmail.Text = memberObj.Email;
                txtEditFName.Text = memberObj.FName;
                txtEditLName.Text = memberObj.LName;
                txtEditPhone.Text = memberObj.Phone;
                if (memberObj.Newsletter > 0)
                    cbEditNewsletter.Checked = true;

                else
                    cbEditNewsletter.Checked = false;
                if (memberObj.SpecialOffer > 0)
                    cbEditSpecialoffer.Checked = true;
                else
                    cbEditSpecialoffer.Checked = false;
            }

        }

        private void btnEditSave_Click(object sender, EventArgs e)
        {
            int id = listMembers.SelectedIndex;
            Member memberObj = memberList[id];
            byte newsletter = 0;
            byte specialoffer = 0;
            if(cbEditNewsletter.Checked)
                newsletter = 1;
            if(cbEditSpecialoffer.Checked)
                specialoffer = 1;

            if (newsletter == memberObj.Newsletter || specialoffer == memberObj.SpecialOffer || txtEditEmail.Text == memberObj.Email || txtEditFName.Text == memberObj.FName || txtEditLName.Text == memberObj.LName || txtEditPhone.Text == memberObj.Phone || txtEditPassword.Text != null)
            {
                memberObj.Email = txtEditEmail.Text;
                memberObj.FName = txtEditFName.Text;
                memberObj.LName = txtEditLName.Text;
                memberObj.Phone = txtEditPhone.Text;
                if(txtEditPassword != null )
                memberObj.Password = CalculateMD5Hash(txtEditPassword.Text);
                if (specialoffer != memberObj.SpecialOffer)
                    memberObj.SpecialOffer = specialoffer;
                if (newsletter != memberObj.SpecialOffer)
                    memberObj.Newsletter = newsletter;

                lblEditResult.Text =  memberCtr.UpdateMember(memberObj);
            }
        }

    }
}
